package com.bitcola.security.activity.biz;


import com.bitcola.entity.SignInLog;
import com.bitcola.security.activity.feign.IDataServiceFeign;
import com.bitcola.security.activity.mapper.ColaUserBalanceMapper;
import com.bitcola.security.activity.mapper.SignInLogMapper;
import com.bitcola.security.auth.common.util.EncoderUtil;
import com.bitcola.security.common.constant.BalanceTypeConstants;
import com.bitcola.security.common.constant.FinancialConstant;
import com.bitcola.security.common.exception.RepeatException;
import com.bitcola.security.common.util.Snowflake;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class SignInLogBiz {
    @Autowired
    SignInLogMapper signInLogMapper;
    ConcurrentHashMap<String,String> map=new ConcurrentHashMap();
    Snowflake snowflake=new Snowflake(4,11);

    @Autowired
    ColaUserBalanceMapper colaUserBalanceMapper;
    @Autowired
    IDataServiceFeign dataServiceFeign;

    @Autowired
    BalanceChangeBiz balanceChangeBiz;
    @Transactional
    public SignInLog signIn(String userId)
    {


        String coinCode="USDT";

        Date now=new Date();

        BigDecimal amount=BigDecimal.valueOf(0.0625);
        userId=map.getOrDefault(userId,userId);
        map.put(userId,userId);
        synchronized (userId) {
            Example example = new Example(SignInLog.class);

            Date dayTime = new Date();
            dayTime.setHours(0);
            dayTime.setMinutes(0);
            dayTime.setSeconds(0);


            example.createCriteria().andGreaterThan("createTime", dayTime).andEqualTo("userId",userId);
            var list = signInLogMapper.selectCountByExample(example);
            if (list > 0) throw new RepeatException("input repeat");
        }

        SignInLog signInLog=new SignInLog();
        signInLog.setId(snowflake.nextIdStr());
        signInLog.setCreateTime(now);
        signInLog.setUserId(userId);
        signInLog.setCoinCode(coinCode);
        signInLog.setAmount(amount);
        signInLog.setCoinCode(coinCode);



        int index= colaUserBalanceMapper.changeUserBanlance(userId,coinCode,amount, BigDecimal.ZERO,amount.negate(),BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO, EncoderUtil.BALANCE_KEY);
        if(index==0){

            signInLog.setAmount(BigDecimal.ZERO);
            signInLogMapper.insert(signInLog);

            return signInLog;
        }
        signInLogMapper.insert(signInLog);

        balanceChangeBiz.createChange(userId, BalanceTypeConstants.prize_balance_frozen,BalanceTypeConstants.prize_balance_frozen,amount.negate(), FinancialConstant.UNLOCKOUT,coinCode);
        balanceChangeBiz.createChange(userId, BalanceTypeConstants.BALANCE,BalanceTypeConstants.prize_balance_frozen,amount, FinancialConstant.UNLOCKIN,coinCode);
        return signInLog;

    }


    public List<SignInLog>query(String userId,int page,int size)
    {

        Example example = new Example(SignInLog.class);


        example.createCriteria().andEqualTo("userId", userId);
        example.orderBy("id").desc();
        RowBounds rowBounds=new RowBounds((page-1)*size,size);
        return signInLogMapper.selectByExampleAndRowBounds(example,rowBounds);

    }


}
